<!DOCTYPE html>
<!--
Copyright 2017 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/model/device.html">
<link rel="import" href="/tracing/model/model.html">
<link rel="import" href="/tracing/model/resource_usage_series.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  const Model = tr.Model;
  const ResourceUsageSeries = tr.model.ResourceUsageSeries;

  test('stableId', function() {
    const device = { stableId: 'test' };
    const series = new ResourceUsageSeries(device);

    assert.strictEqual(series.stableId, 'test.ResourceUsageSeries');
  });

  test('device', function() {
    const device = new tr.model.Device(new Model());
    const series = new ResourceUsageSeries(device);

    assert.strictEqual(series.device, device);
  });

  test('addUsageSample', function() {
    const series = new ResourceUsageSeries(new Model().device);

    assert.strictEqual(series.samples.length, 0);

    const sample1 = series.addUsageSample(0, 1);
    const sample2 = series.addUsageSample(1, 2);

    assert.deepEqual(series.samples, [sample1, sample2]);
  });

  test('getResourceTimeConsumed_oneInterval', function() {
    const series = new ResourceUsageSeries(new Model().device);
    series.addUsageSample(0, 1);
    series.addUsageSample(1000, 2);

    assert.strictEqual(series.computeResourceTimeConsumedInMs(0, 1000), 1000);
  });

  test('getResourceTimeConsumed_twoIntervals', function() {
    const series = new ResourceUsageSeries(new Model().device);
    series.addUsageSample(0, 1);
    series.addUsageSample(1000, 2);
    series.addUsageSample(2000, 2);

    assert.strictEqual(series.computeResourceTimeConsumedInMs(0, 2000), 3000);
  });

  test('getResourceTimeConsumed_oneSample', function() {
    const series = new ResourceUsageSeries(new Model().device);
    series.addUsageSample(1000, 1);

    assert.strictEqual(series.computeResourceTimeConsumedInMs(0, 2000), 0);
  });

  test('getResourceTimeConsumed_samplesAfterStart', function() {
    const series = new ResourceUsageSeries(new Model().device);
    series.addUsageSample(1000, 1);
    series.addUsageSample(2000, 2);

    assert.strictEqual(series.computeResourceTimeConsumedInMs(0, 2000), 1000);
  });

  test('getResourceTimeConsumed_extraSamplesBeforeStart', function() {
    const series = new ResourceUsageSeries(new Model().device);
    series.addUsageSample(0, 10);
    series.addUsageSample(1000, 1);
    series.addUsageSample(2000, 1);
    series.addUsageSample(3000, 1);

    assert.strictEqual(
        series.computeResourceTimeConsumedInMs(2000, 4000), 1000);
  });

  test('getResourceTimeConsumed_extraSamplesAfterEnd', function() {
    const series = new ResourceUsageSeries(new Model().device);
    series.addUsageSample(0, 1);
    series.addUsageSample(1000, 1);
    series.addUsageSample(2000, 1);
    series.addUsageSample(3000, 10);

    assert.strictEqual(series.computeResourceTimeConsumedInMs(0, 2000), 2000);
  });

  test('shiftTimestampsForward', function() {
    const series = new ResourceUsageSeries(new Model().device);

    series.addUsageSample(0, 1);
    series.addUsageSample(1, 2);

    series.shiftTimestampsForward(2);

    assert.strictEqual(series.samples[0].start, 2);
    assert.strictEqual(series.samples[1].start, 3);

    series.shiftTimestampsForward(-4);

    assert.strictEqual(series.samples[0].start, -2);
    assert.strictEqual(series.samples[1].start, -1);
  });


  test('updateBounds', function() {
    const series = new ResourceUsageSeries(new Model().device);

    series.addUsageSample(0, 1);
    series.addUsageSample(1, 2);
    series.updateBounds();

    assert.strictEqual(series.bounds.min, 0);
    assert.strictEqual(series.bounds.max, 1);

    series.addUsageSample(4, 3);
    series.updateBounds();

    assert.strictEqual(series.bounds.min, 0);
    assert.strictEqual(series.bounds.max, 4);
  });

  test('childEvents_empty', function() {
    const series = new ResourceUsageSeries(new Model().device);
    const eventsInSeries = [];
    for (const event of series.childEvents()) {
      eventsInSeries.push(event);
    }
    assert.deepEqual(eventsInSeries, []);
  });

  test('childEvents_nonempty', function() {
    const series = new ResourceUsageSeries(new Model().device);
    const sample1 = series.addUsageSample(0, 1);
    const sample2 = series.addUsageSample(1, 2);
    const eventsInSeries = [];
    for (const event of series.childEvents()) {
      eventsInSeries.push(event);
    }
    assert.deepEqual(eventsInSeries, [sample1, sample2]);
  });
});
</script>
